package com.fr.report.script.function;

import com.fr.base.core.DateUtils;
import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import java.util.Date;

public class DATEINMONTH extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 1)
      return Primitive.ERROR_NAME;
    Date localDate = null;
    int i = 0;
    if ((paramArrayOfObject.length == 1) && (paramArrayOfObject[0] instanceof Number))
    {
      localDate = new Date();
      i = ((Number)paramArrayOfObject[0]).intValue();
    }
    else if ((paramArrayOfObject.length >= 2) && (paramArrayOfObject[1] instanceof Number))
    {
      i = ((Number)paramArrayOfObject[1]).intValue();
      if (paramArrayOfObject[0] instanceof Date)
        localDate = (Date)paramArrayOfObject[0];
      else if (paramArrayOfObject[0] instanceof String)
        localDate = DateUtils.object2Date((String)paramArrayOfObject[0], false);
    }
    if (localDate == null)
      localDate = new Date();
    if (i == 0)
      return localDate;
    int j = localDate.getMonth();
    int k = (i > 0) ? j : j + 1;
    int l = (i > 0) ? i : 1 + i;
    return new Date(localDate.getYear(), k, l);
  }

  public Function.Type getType()
  {
    return Function.DATETIME;
  }

  public String getCN()
  {
    return "DATEINMONTH(date, number):函数返回在某一个月当中第几天的日期。\n示例：\nDATEINMONTH(\"2008-08-08\", 20) 等于2008-08-20。\nDATEINMONTH(\"2008-08-08\", -1) 等于2008-08-31。";
  }

  public String getEN()
  {
    return "DATEINMONTH(date, number):return the date of one day in a month.\nExample:\nDATEINMONTH(\"2008-08-08\", 20) = 2008-08-20。\nDATEINMONTH(\"2008-08-08\", -1) = 2008-08-31。";
  }
}